﻿/*
 * Created by SharpDevelop.
 * User: CrOnO
 * Date: 28/10/2011
 * Time: 11:55 a.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Business_Entity;

namespace DAOFactory
{
	/// <summary>
	/// Description of DAOTurno.
	/// </summary>
	public class DAOTurno
	{
		public DAOTurno()
		{
		}
		
		public List<BETurno> obtenerTurno(BEEmpleado empleado)
		{
			try
			{
				SqlCommand cmd = new SqlCommand("RRHH_BUSCAR_TURNO_L", ConnectionManager.getConnection());
				cmd.CommandType = CommandType.StoredProcedure;
				
				cmd.Parameters.Add("@IDHORARIO", SqlDbType.Int);
				cmd.Parameters["@IDHORARIO"].Value = empleado.IdHorario;
				cmd.Parameters.Add("@ESTADOTRAN", SqlDbType.Int);
				cmd.Parameters["@ESTADOTRAN"].Direction = ParameterDirection.Output;
				
				object estadoTran;
				
				SqlDataReader amd = cmd.ExecuteReader();			
				estadoTran = cmd.Parameters["@ESTADOTRAN"].Value;
				
				List<BETurno> lista = new List<BETurno>();
				
				if (amd.HasRows) 
				{
					while(amd.Read())
					{
						try
						{
							if ((amd[2] != DBNull.Value) && (amd[3] != DBNull.Value))
							{
								BETurno turno = new BETurno(int.Parse(amd[0].ToString()),
								                            int.Parse(amd[1].ToString()),
								                            DateTime.Parse(amd[2].ToString()),
								                            DateTime.Parse(amd[3].ToString()));
								//System.Windows.Forms.MessageBox.Show(DateTime.Parse(amd[2].ToString()).ToString(),"?");
								lista.Add(turno);	
							}
							else if ((amd[2] == DBNull.Value) && (amd[3] == DBNull.Value))
							{
								BETurno turno = new BETurno(int.Parse(amd[0].ToString()),
								                            int.Parse(amd[1].ToString()));
								turno.HoraEntrada = DateTime.MinValue;
								turno.HoraSalida = DateTime.MinValue;
								lista.Add(turno);
							}
							
						}
						catch
						{
//							System.Windows.Forms.MessageBox.Show("Error al buscar turnos","Error");
							amd.Close();
							return null;						
						}
					}
					amd.Close();
					return lista;
				}
				else
				{
					amd.Close();
					return null;
				}
			}
			catch
			{
//				System.Windows.Forms.MessageBox.Show("Error al buscar turnos","Error");
				return null;
			}
		}
		
		public int modificarTurno(List<DateTime> horas, int idHorario)
		{
			int estadoTran = 0;
			
			try
			{				
				SqlCommand cmd = new SqlCommand("RRHH_MODIFICAR_TURNO_L", ConnectionManager.getConnection());
				cmd.CommandType = CommandType.StoredProcedure;
				
				cmd.Parameters.Add("@IDHORARIO", SqlDbType.Int);
				cmd.Parameters["@IDHORARIO"].Value = idHorario;
				
				if ((horas[0] != DateTime.MinValue) && (horas[1] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HLUNESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HLUNESENTRADA"].Value = horas[0];
					cmd.Parameters.Add("@HLUNESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HLUNESSALIDA"].Value = horas[1];
				}
				else
				{
					cmd.Parameters.Add("@HLUNESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HLUNESENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HLUNESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HLUNESSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[2] != DateTime.MinValue) && (horas[3] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HMARTESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HMARTESENTRADA"].Value = horas[2];
					cmd.Parameters.Add("@HMARTESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HMARTESSALIDA"].Value = horas[3];
				}
				else
				{
					cmd.Parameters.Add("@HMARTESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HMARTESENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HMARTESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HMARTESSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[4] != DateTime.MinValue) && (horas[5] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HMIERCOLESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HMIERCOLESENTRADA"].Value = horas[4];
					cmd.Parameters.Add("@HMIERCOLESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HMIERCOLESSALIDA"].Value = horas[5];
				}
				else
				{
					cmd.Parameters.Add("@HMIERCOLESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HMIERCOLESENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HMIERCOLESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HMIERCOLESSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[6] != DateTime.MinValue) && (horas[7] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HJUEVESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HJUEVESENTRADA"].Value = horas[6];
					cmd.Parameters.Add("@HJUEVESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HJUEVESSALIDA"].Value = horas[7];
				}
				else
				{
					cmd.Parameters.Add("@HJUEVESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HJUEVESENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HJUEVESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HJUEVESSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[8] != DateTime.MinValue) && (horas[9] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HVIERNESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HVIERNESENTRADA"].Value = horas[8];
					cmd.Parameters.Add("@HVIERNESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HVIERNESSALIDA"].Value = horas[9];
				}
				else
				{
					cmd.Parameters.Add("@HVIERNESENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HVIERNESENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HVIERNESSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HVIERNESSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[10] != DateTime.MinValue) && (horas[11] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HSABADOENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HSABADOENTRADA"].Value = horas[10];
					cmd.Parameters.Add("@HSABADOSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HSABADOSALIDA"].Value = horas[11];
				}
				else
				{
					cmd.Parameters.Add("@HSABADOENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HSABADOENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HSABADOSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HSABADOSALIDA"].Value = DBNull.Value;	
				}
				
				if ((horas[12] != DateTime.MinValue) && (horas[13] != DateTime.MinValue))
				{
					cmd.Parameters.Add("@HDOMINGOENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HDOMINGOENTRADA"].Value = horas[12];
					cmd.Parameters.Add("@HDOMINGOSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HDOMINGOSALIDA"].Value = horas[13];
				}
				else
				{
					cmd.Parameters.Add("@HDOMINGOENTRADA", SqlDbType.DateTime);
					cmd.Parameters["@HDOMINGOENTRADA"].Value = DBNull.Value;
					cmd.Parameters.Add("@HDOMINGOSALIDA", SqlDbType.DateTime);
					cmd.Parameters["@HDOMINGOSALIDA"].Value = DBNull.Value;	
				}

				cmd.Parameters.Add("@ESTADOTRAN", SqlDbType.Int);
				cmd.Parameters["@ESTADOTRAN"].Direction = ParameterDirection.Output;
					
				cmd.ExecuteNonQuery();
				estadoTran = (int) cmd.Parameters["@ESTADOTRAN"].Value;
				//ConnectionManager.closeConnection();
			}
			catch (Exception e)
			{
//				System.Windows.Forms.MessageBox.Show("Error al modificar turnos"+e,"Error");
			}
			
			return estadoTran;
		}
	}
}
